/*
 * (C) APUE
 * pthread_cleanup_push() / pthread_cleanup_pop()
 * Three situations will call the cleanup handlers
 *	1. call pthread_exit
 *	2. response to pthread_cancel
 *	3. pthread_cleanup_pop with nonzero argument
 */
#include "apue.h"
#include <pthread.h>

void cleanup(void *arg)
{
	printf("cleanup: %s\n", (char*)arg);
}

void *thr_fn1(void *arg)
{
	printf("thread 1 start\n");
	pthread_cleanup_push(cleanup, "thread 1 first handler");
	pthread_cleanup_push(cleanup, "thread 1 second handler");
	printf("thread 1 push complete\n");
	if (arg)
		return ((void*)1);	/* return in thread main handler, cleanup will not be executed */
	pthread_cleanup_pop(0);
	pthread_cleanup_pop(0);
	return ((void*)1);
}

void *thr_fn2(void *arg)
{
	printf("thread 2 start\n");
	pthread_cleanup_push(cleanup, "thread 2 first handler");
	pthread_cleanup_push(cleanup, "thread 2 second handler");
	printf("thread 2 push complete\n");
	if (arg)
		pthread_exit((void*)2); /* note that here is pthread_exit, not just return */
	pthread_cleanup_pop(0);
	pthread_cleanup_pop(0);
	return ((void*)2);
}

int main()
{
	int err;
	pthread_t tid1, tid2;
	void *tret;

	err = pthread_create(&tid1, NULL, thr_fn1, (void*)1);
	if (err != 0)
		err_quit("can't create thread 1: %s\n", strerror(err));
	err = pthread_create(&tid2, NULL, thr_fn2, (void*)1);
	if (err != 0)
		err_quit("can't create thread 2: %s\n", strerror(err));

	err = pthread_join(tid1, &tret);
	if (err != 0)
		err_quit("can't join with thread 1: %s\n", strerror(err));
	printf("thread 1 exit code %d\n", (int)tret);

	err = pthread_join(tid2, &tret);
	if (err != 0)
		err_quit("can't join with thread 2: %s\n", strerror(err));
	printf("thread 2 exit code %d\n", (int)tret);
	
	exit(0);
}
